package com.zego.wawaji.ui.dialog;

import android.app.Activity;
import android.support.v4.app.FragmentActivity;

import com.zego.wawaji.R;
import com.zego.wawaji.base.bean.Updata;
import com.zego.wawaji.base.module.ModuleMgr;
import com.zego.wawaji.base.toast.PToast;
import com.zego.wawaji.ui.Fragment.play.EveryGiftBagDialog;
import com.zego.wawaji.ui.bean.pay.Pay;
import com.zego.wawaji.ui.center.pay.award.PayGetCoinDialog;
import com.zego.wawaji.ui.center.wawa.WawaExchangeDialog;
import com.zego.wawaji.ui.coupon.bean.Coupon;
import com.zego.wawaji.ui.coupon.dialog.CouponBottomDialog;
import com.zego.wawaji.ui.coupon.dialog.CouponDialog;
import com.zego.wawaji.ui.dialog.appraise.AppraiseDialog;
import com.zego.wawaji.ui.dialog.award.SendAwardDialog;
import com.zego.wawaji.ui.dialog.game.StartGameDialog;
import com.zego.wawaji.ui.dialog.invite.InviteCodeDialog;
import com.zego.wawaji.ui.dialog.pay.PayAwardDialog;
import com.zego.wawaji.ui.dialog.pay.PayBottomDialog;
import com.zego.wawaji.ui.dialog.pay.PayDialog;
import com.zego.wawaji.ui.dialog.share.InviteDialog;
import com.zego.wawaji.ui.dialog.share.ShareDialog;
import com.zego.wawaji.ui.dialog.updata.UpdateDialog;
import com.zego.wawaji.ui.start.LoginTestDialog;
import com.zego.wawaji.ui.wawa.synth.SynthExplainDialog;
import com.zego.wawaji.ui.wawa.synth.material.MaterialExchangDialog;

import java.util.List;

/**
 * Dialog工具类
 */
public class PickerDialogUtil {

    /**
     * 文字提示Dialog
     *
     * @param activity
     * @param listener 取消/确定按钮回调
     * @param content  对话框提示内容
     * @param title    对话框标题
     * @param isBoth   是否双按钮显示，true : 双按钮
     */
    public static void showSimpleTipDialog(FragmentActivity activity, SimpleTipDialog.ConfirmListener listener, CharSequence content, CharSequence title, boolean isBoth) {
        showSimpleTipDialog(activity, listener, content, title, "取消", "确定", isBoth);
    }

    /**
     * 文字提示Dialog，可设置是否点击返回键消失，其他参数说明见{@link #showSimpleTipDialog}
     *
     * @param isCancelable false:不消失
     */
    public static void showTipDialogCancelBack(FragmentActivity activity, SimpleTipDialog.ConfirmListener listener, CharSequence content, CharSequence title, String cancelTxt, String submitTxt, boolean isBoth, boolean isCancelable) {
        SimpleTipDialog tipDialog = new SimpleTipDialog(activity);
        tipDialog.setTitleText(title);
        tipDialog.setContentString(content);
        tipDialog.setCancelText(cancelTxt);
        tipDialog.setSubmitText(submitTxt);
        tipDialog.setCancelVisible(isBoth);
        tipDialog.setConfirmListener(listener);
        tipDialog.setDialogCancelable(isCancelable);
        tipDialog.show();
    }

    /**
     * 显示普通提示dialog
     *
     * @param activity  FragmentActivity实例
     * @param listener  按钮点击监听
     * @param content   提示文字内容
     * @param title     dialog标题
     * @param cancelTxt 取消按钮文字
     * @param submitTxt 确定按钮文字
     * @param isBoth    是否显示两个按钮，如果为false，则默认值显示确认按钮
     */
    public static void showSimpleTipDialog(FragmentActivity activity, SimpleTipDialog.ConfirmListener listener, CharSequence content, CharSequence title, String cancelTxt, String submitTxt, boolean isBoth) {
        SimpleTipDialog tipDialog = new SimpleTipDialog(activity);
        tipDialog.setTitleText(title);
        tipDialog.setContentString(content);
        tipDialog.setCancelText(cancelTxt);
        tipDialog.setSubmitText(submitTxt);
        tipDialog.setCancelVisible(isBoth);
        tipDialog.setConfirmListener(listener);
        tipDialog.show();
    }

    /**
     * 显示普通提示dialog
     *
     * @param activity  FragmentActivity实例
     * @param listener  按钮点击监听
     * @param content   提示文字内容
     * @param title     dialog标题
     * @param cancelTxt 取消按钮文字
     * @param submitTxt 确定按钮文字
     * @param isBoth    是否显示两个按钮，如果为false，则默认值显示确认按钮
     * @param color     文字内容颜色
     */
    public static void showSimpleTipDialogExt(FragmentActivity activity, SimpleTipDialog.ConfirmListener listener, CharSequence content, CharSequence title, String cancelTxt, String submitTxt, boolean isBoth, int color) {
        SimpleTipDialog tipDialog = new SimpleTipDialog(activity);
        tipDialog.setTitleText(title);
        tipDialog.setContentString(content);
        tipDialog.setContentColor(color);
        tipDialog.setCancelText(cancelTxt);
        tipDialog.setSubmitText(submitTxt);
        tipDialog.setCancelVisible(isBoth);
        tipDialog.setConfirmListener(listener);
        tipDialog.show();
    }

    /**
     * 简单提示对话框（退出，删除照片等）
     */
    public static void showSimpleAlertDialog(FragmentActivity activity, SimpleTipDialog.ConfirmListener listener, CharSequence content, CharSequence title) {
        showSimpleAlertDialog(activity, listener, content, R.color.txt_color1, title);
    }

    /**
     * 简单提示对话框,可自定义提示文字颜色
     */
    public static void showSimpleAlertDialog(FragmentActivity activity, SimpleTipDialog.ConfirmListener listener, CharSequence content, int color, CharSequence title) {
        SimpleTipDialog tipDialog = new SimpleTipDialog(activity);
        tipDialog.setTitleText(title);
        tipDialog.setContentString(content);
        tipDialog.setContentColor(color);
        tipDialog.setConfirmListener(listener);
        tipDialog.show();
    }

    /**
     * 展示升级弹框
     */
    public static void showUpdateDialog(FragmentActivity activity, Updata appUpdate) {
        UpdateDialog updateDialog = new UpdateDialog();
        updateDialog.setData(appUpdate);
        updateDialog.showDialog(activity);
    }

    /**
     * 展示分享弹框
     */
    public static void showShareDialog(FragmentActivity activity) {
        ShareDialog shareDialog = new ShareDialog();
        if (activity instanceof ShareDialog.OnShareClickListener) {
            shareDialog.setOnShareListener((ShareDialog.OnShareClickListener) activity);
        }
        shareDialog.showDialog(activity);
    }

    /**
     * 娃娃兑换娃娃币成功弹框
     *
     * @param awardNum 奖励娃娃币
     */
    public static void showWawaExchangeDialog(FragmentActivity activity, int awardNum) {
        WawaExchangeDialog dialog = new WawaExchangeDialog(activity);
        dialog.setAwardNum(awardNum);
        dialog.show();
    }

    /**
     * 开始游戏倒计时弹框
     */
    public static void showStartGameDialog(FragmentActivity activity, int rspSeq, String sessionData) {
        StartGameDialog dialog = new StartGameDialog(activity);
        dialog.setData(rspSeq, sessionData);
        dialog.show();
    }

    /**
     * 成功被邀请弹框
     */
    public static void showInviteDialog(FragmentActivity activity, int awardNum) {
        InviteDialog dialog = new InviteDialog(activity);
        dialog.setAwardNum(awardNum);
        dialog.show();
    }

    /**
     * 输入验证码弹框
     */
    public static void showInviteCodeDialog(FragmentActivity activity) {
        InviteCodeDialog dialog = new InviteCodeDialog(activity);
        dialog.show();
    }

    /**
     * 登录测试弹框
     */
    public static void showLoginTestDialog(FragmentActivity activity) {
        LoginTestDialog dialog = new LoginTestDialog(activity);
        dialog.show();
    }

    /**
     * 充值方式弹框
     */
    private static PayBottomDialog bottomDialog;
    private static Pay payGoods;

    public static void showPayBottomDialog(Activity activity, final Pay pay) {
        payGoods = pay;
        bottomDialog = new PayBottomDialog(activity, (parent, view, position, id, itemInfos) -> {
            pay(position);
            bottomDialog.dismissDialog();
        });
        bottomDialog.showDialog();
    }

    /**
     * 开通会员充值方式弹框
     */
    public static void showPayVipDialog(Activity activity, final Pay pay) {
        payGoods = pay;
        bottomDialog = new PayBottomDialog(activity, (parent, view, position, id, itemInfos) -> {
            payVip(position);
            bottomDialog.dismissDialog();
        });
        bottomDialog.showDialog();
    }

    /**
     * 使用优惠券充值方式弹框
     */
    public static void showPayCouponDialog(Activity activity, final Pay pay, final Coupon couponData) {
        payGoods = pay;
        bottomDialog = new PayBottomDialog(activity, (parent, view, position, id, itemInfos) -> {
            if (couponData == null) {
                pay(position);
            } else {
                payCoupon(position, couponData);
            }
            bottomDialog.dismissDialog();
        });
        bottomDialog.showDialog();
    }

    /**
     * 充值商品列表弹框
     */
    private static PayDialog payDialog;

    public static void showPayDialog(FragmentActivity activity) {
        payDialog = new PayDialog(activity);
        payDialog.show();
    }

    public static void showPayDialogCoupon(FragmentActivity activity, Coupon data) {
        payDialog = new PayDialog(activity, data);
        payDialog.show();
    }

    public static void closePayDialog() {
        try {
            if (payDialog != null) {
                payDialog.dismiss();
            }
        } catch (Exception e) {
        }
    }

    private static void pay(int position) {
        if (payGoods == null) {
            PToast.showShort("获取商品信息失败，请稍后重试");
            return;
        }

        if (position == 0) {
            ModuleMgr.getWawaMgr().reqWXPay("金币" + (payGoods.getWwCoinCz() + payGoods.getWwCoinZs()) + "个", payGoods.getId(), null);
        } else if (position == 1) {
            ModuleMgr.getWawaMgr().reqAlipay("金币" + (payGoods.getWwCoinCz() + payGoods.getWwCoinZs()) + "个", payGoods.getId(), null);
        }
    }

    private static void payVip(int position) {
        if (payGoods == null) {
            PToast.showShort("获取商品信息失败，请稍后重试");
            return;
        }
        if (position == 0) {
            ModuleMgr.getWawaMgr().reqWXPay("开通VIP", payGoods.getId(), null);
        } else if (position == 1) {
            ModuleMgr.getWawaMgr().reqAlipay("开通VIP", payGoods.getId(), null);
        }
    }

    // 使用优惠券充值
    private static void payCoupon(int position, Coupon couponData) {
        if (payGoods == null) {
            PToast.showShort("获取商品信息失败，请稍后重试");
            return;
        }
        if (position == 0) {
            ModuleMgr.getWawaMgr().reqWXPay("金币" + (payGoods.getWwCoinCz() + payGoods.getWwCoinZs()) + "个，赠送娃娃币" + couponData.getWwCoinZs() + "个",
                    payGoods.getId(), couponData.getCouponId(), null);
        } else if (position == 1) {
            ModuleMgr.getWawaMgr().reqAlipay("金币" + (payGoods.getWwCoinCz() + payGoods.getWwCoinZs()) + "个，赠送娃娃币" + couponData.getWwCoinZs() + "个",
                    payGoods.getId(), couponData.getCouponId(), null);
        }
    }

    /**
     * 首冲宝箱
     */
    public static void showPayAwardDialog(FragmentActivity activity, String content) {
        PayAwardDialog dialog = new PayAwardDialog(activity, content);
        dialog.show();
    }

    /**
     * 获取优惠券弹框
     */
    public static void showCouponDialog(FragmentActivity activity, Coupon coupon) {
        CouponDialog dialog = new CouponDialog(activity, coupon);
        dialog.show();
    }

    /**
     * 充值方式弹框
     */
    private static CouponBottomDialog couponBottomDialog;

    public static void showCouponBottomDialog(Activity activity, List<Coupon> list, Pay pay) {
        couponBottomDialog = new CouponBottomDialog(activity, list, pay);
        couponBottomDialog.showDialog();
    }

    /**
     * 娃娃合成说明
     */
    public static void showSynthExplainDialog(FragmentActivity activity) {
        SynthExplainDialog dialog = new SynthExplainDialog(activity);
        dialog.show();
    }

    /**
     * 兑换合成材料成功
     */
    public static void showMaterialExchangeDialog(FragmentActivity activity) {
        MaterialExchangDialog dialog = new MaterialExchangDialog(activity);
        dialog.show();
    }

    /**
     * 7日领币成功弹框
     */
    public static void showPayGetCoinDialog(FragmentActivity activity, int awardCoin) {
        PayGetCoinDialog dialog = new PayGetCoinDialog(activity, awardCoin);
        dialog.show();
    }

    /**
     * 每日礼包弹框
     */
    public static void showEveryGiftBagDialog(FragmentActivity activity) {
        EveryGiftBagDialog dialog = new EveryGiftBagDialog(activity);
        dialog.show();
    }

    /**
     * 评价弹框
     */
    public static void showAppraiseDialog(FragmentActivity activity) {
        AppraiseDialog dialog = new AppraiseDialog(activity);
        dialog.show();
    }

    /**
     * 发货奖励弹框
     */
    public static void showSendAwardDialog(FragmentActivity activity, int awardNum) {
        SendAwardDialog dialog = new SendAwardDialog(activity);
        dialog.setAwardNum(awardNum);
        dialog.show();
    }
}
